package com.example.h1nson.realphotography.viewpager;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.h1nson.realphotography.R;
import com.example.h1nson.realphotography.activity.MainActivity;
import com.example.h1nson.realphotography.activity.WelcomeActivity;
import com.example.h1nson.realphotography.asyncTask.HomeInfoAsynTask;
import com.example.h1nson.realphotography.config.ConfigURL;
import com.example.h1nson.realphotography.config.GetCurrentUser;
import com.example.h1nson.realphotography.entity.User;
import com.example.h1nson.realphotography.interface_photo.OnDataFinishedListener;
import com.example.h1nson.realphotography.interface_photo.OnHomeInfoClickListener;
import com.example.h1nson.realphotography.util.AsyncImageLoader;
import com.shelwee.uilistview.model.BasicItem;
import com.shelwee.uilistview.ui.UiListView;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Created by H1nson on 2016/5/4.
 */
public class MainTab04 extends Fragment
{
    private OnHomeInfoClickListener onHomeInfoClickListener;
    private UiListView uiListView0,uiListView1, uiListView2, uiListView3, uiListView4;
    private TextView textViewInfo,textViewFansNum;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        View viewHome = inflater.inflate(R.layout.main_tab_04, container, false);
        uiListView0 = (UiListView)viewHome.findViewById(R.id.uilist0);
        //uiListView1 = (UiListView)viewHome.findViewById(R.id.uilist1);
        uiListView2 = (UiListView)viewHome.findViewById(R.id.uilist2);
        uiListView3 = (UiListView)viewHome.findViewById(R.id.uilist3);
        uiListView4 = (UiListView)viewHome.findViewById(R.id.uilist4);
        textViewInfo = (TextView)viewHome.findViewById(R.id.textviewInfo);
        textViewFansNum = (TextView)viewHome.findViewById(R.id.textviewFansNum);
        getHomeInfo();
        return viewHome;
    }
    public void setOnHomeInfoClickListener(OnHomeInfoClickListener onHomeInfoClickListener){
        this.onHomeInfoClickListener = onHomeInfoClickListener;
    }
    public void getHomeInfo(){
        HomeInfoAsynTask homeInfoAsynTask = new HomeInfoAsynTask(GetCurrentUser.userEmail);
        homeInfoAsynTask.setOnDataFinishedListener(new OnDataFinishedListener() {
            @Override
            public void onDataSuccessfully(Object data) {
                try {
                    JSONObject json = new JSONObject((String)data);
                    initUilist0();
                    initUilist1(json.getString("userInfo"), json.getInt("userFansNum"));
                    initUilist2();
                    initUilist3();
                    initUilist4();
                }catch (JSONException e){
                    e.printStackTrace();
                }
            }
            @Override
            public void onDataFailed() {
//                initUilist0("loading","loading");
//                initUilist1("loading",0);
//                initUilist2();
//                initUilist3();
//                initUilist4();
            }
        });
        homeInfoAsynTask.execute();
    }
    public void initUilist4() {
        // TODO Auto-generated method stub
        uiListView4.addBasicItem("退出登录");
        uiListView4.commit();
        uiListView4.setClickListener(new UiListView.ClickListener() {

            @Override
            public void onClick(int index) {
                onHomeInfoClickListener.changeUI(8);
            }
        });
    }

    public void initUilist3() {
        // TODO Auto-generated method stub
        uiListView3.addBasicItem("修改个人信息");
        uiListView3.addBasicItem("我的位置");
        uiListView3.addBasicItem("关于RealPhotography");
        uiListView3.commit();
        uiListView3.setClickListener(new UiListView.ClickListener() {

            @Override
            public void onClick(int index) {
                switch (index){
                    case 0:
                        onHomeInfoClickListener.changeUI(5);
                        break;
                    case 1:
                        onHomeInfoClickListener.changeUI(6);
                        break;
                    case 2:
                        onHomeInfoClickListener.changeUI(7);
                        break;
                }

            }
        });
    }

    public void initUilist2() {
        // TODO Auto-generated method stub
        uiListView2.addBasicItem("关注");
        uiListView2.addBasicItem("我的图片");
        uiListView2.addBasicItem("收藏");
        uiListView2.addBasicItem("浏览历史");
        uiListView2.commit();
        uiListView2.setClickListener(new UiListView.ClickListener() {

            @Override
            public void onClick(int index) {
                switch (index){
                    case 0:
                        onHomeInfoClickListener.changeUI(1);
                        break;
                    case 1:
                        onHomeInfoClickListener.changeUI(2);
                        break;
                    case 2:
                        onHomeInfoClickListener.changeUI(3);
                        break;
                    case 3:
                        onHomeInfoClickListener.changeUI(4);
                        break;
                }

            }
        });
    }

    public void initUilist1(String userInfo,int fansNum) {
       // uiListView1.addBasicItem("世界那么大三天假太少，钱包那么小哪也去不了");
        if(userInfo.equals("noInfo")){
            userInfo="这个人很懒，什么简介都没留下";
        }
//        uiListView1.addBasicItem(userInfo);
//        uiListView1.addBasicItem("fans:"+fansNum);
//        uiListView1.commit();
//        uiListView1.setClickListener(new UiListView.ClickListener() {
//
//            @Override
//            public void onClick(int index) {
//                // TODO Auto-generated method stub
//                //Toast.makeText(MainActivity.this, "点击"+index, Toast.LENGTH_SHORT).show();
//            }
//        });
        textViewInfo.setText(userInfo);
        textViewFansNum.setText("Fans:"+fansNum+"人");

    }
    public void initUilist0() {
        if(GetCurrentUser.currentUser.getUserHead().toString().equals("noHead")) {
            AsyncImageLoader asyncImageLoader = new AsyncImageLoader();
            String imageUrl = ConfigURL.headURL+"default_head.png";
            Drawable cachedImage = asyncImageLoader.loadDrawable(imageUrl, new AsyncImageLoader.ImageCallback() {
                public void imageLoaded(Drawable imageDrawable, String imageUrl) {
                    if (imageDrawable == null) {
                        //imageView.setImageResource(R.drawable.icon);
                        imageDrawable = getResources().getDrawable(R.drawable.user_image);
                    }
                    uiListView0.addBasicItem(new BasicItem(imageDrawable, GetCurrentUser.currentUser.getUserName()));
                    uiListView0.commit();
                }
            });
            if (cachedImage == null) {
                cachedImage = getResources().getDrawable(R.drawable.headpic);
                Log.e("Adapter", "null");
            }
        }
        else {
            AsyncImageLoader asyncImageLoader = new AsyncImageLoader();
            String imageUrl = ConfigURL.headURL+GetCurrentUser.currentUser.getUserHead();
            Drawable cachedImage = asyncImageLoader.loadDrawable(imageUrl, new AsyncImageLoader.ImageCallback() {
                public void imageLoaded(Drawable imageDrawable, String imageUrl) {

                    if (imageDrawable == null) {
                        //imageView.setImageResource(R.drawable.icon);
                        imageDrawable = getResources().getDrawable(R.drawable.user_image);
                    }
                    uiListView0.addBasicItem(new BasicItem(imageDrawable, GetCurrentUser.currentUser.getUserName()));
                    uiListView0.commit();
                }
            });
            if (cachedImage == null) {
                cachedImage = getResources().getDrawable(R.drawable.headpic);
                Log.e("Adapter", "null");
            }
            uiListView0.setClickListener(new UiListView.ClickListener() {

                @Override
                public void onClick(int index) {
                    onHomeInfoClickListener.changeUI(0);
                }
            });
        }
    }
    public void setTextViewInfo(String info){
        textViewInfo.setText(info);
    }
    public UiListView getUiListView0(){
        return  uiListView0;
    }
}
